package springboot.springboot06_ssmp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import springboot.springboot06_ssmp.entity.PersonalDiaries;
import springboot.springboot06_ssmp.mapper.PersonalDiariesMapper;
import springboot.springboot06_ssmp.service.IPersonalDiariesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 个人日记 服务实现类
 * </p>
 *
 * @author daiqiang
 * @since 2024-02-23
 */
@Service
public class PersonalDiariesServiceImpl extends ServiceImpl<PersonalDiariesMapper, PersonalDiaries> implements IPersonalDiariesService {
    @Autowired
    private PersonalDiariesMapper personalDiariesMapper;

    @Override
    public IPage getPage(int currentPage, int pageSize, PersonalDiaries personalDiaries) {

        LambdaQueryWrapper<PersonalDiaries> lqw = new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(personalDiaries.getTitle()),PersonalDiaries::getTitle,personalDiaries.getTitle());
        lqw.like(Strings.isNotEmpty(personalDiaries.getContent()),PersonalDiaries::getContent,personalDiaries.getContent());
        // 处理日期字段
        if (personalDiaries.getCreateDate() != null) {
            // 获取查询开始日期（当天凌晨）
            Date startDate = Date.from(personalDiaries.getCreateDate().atStartOfDay(ZoneId.systemDefault()).toInstant());
            // 获取查询结束日期（第二天凌晨，即当前日期范围的最后一天）
            Date endDate = Date.from(personalDiaries.getCreateDate().plusDays(1).atStartOfDay(ZoneId.systemDefault()).toInstant());
            lqw.between(PersonalDiaries::getCreateDate, startDate, endDate);

        }
        lqw.like(Strings.isNotEmpty(personalDiaries.getTags()),PersonalDiaries::getTags,personalDiaries.getTags());
        lqw.like(Strings.isNotEmpty(personalDiaries.getMood()),PersonalDiaries::getMood,personalDiaries.getMood());

        IPage page = new Page(currentPage,pageSize);
        personalDiariesMapper.selectPage(page,lqw);
        return  page;
    }
}
